package com.ruoyi.web.controller.eduManage;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.eduManage.domain.EduClass;
import com.ruoyi.eduManage.domain.EduTeacher;
import com.ruoyi.eduManage.domain.vo.EduClassVO;
import com.ruoyi.eduManage.service.IEduClassService;
import com.ruoyi.eduManage.service.IEduTeacherService;
import com.ruoyi.eduManage.utils.EduDictUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;

/**
 * 班级信息Controller
 * 
 * @author ruoyi
 * @date 2025-10-24
 */
@RestController
@RequestMapping("/manage/class")
public class EduClassController extends BaseController
{
    @Autowired
    private IEduClassService eduClassService;
    
    @Autowired
    private IEduTeacherService eduTeacherService;

    /**
     * 查询班级信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduClass eduClass)
    {
        startPage();
        List<EduClass> list = eduClassService.selectEduClassList(eduClass);
        TableDataInfo tableDataInfo = getDataTable(list);
        // 转换为VO对象，并添加教师姓名
        List<EduClassVO> voList = new ArrayList<>();
        for (EduClass clazz : list) {
            EduClassVO vo = new EduClassVO();
            org.springframework.beans.BeanUtils.copyProperties(clazz, vo);
            
            // 处理关班状态字典映射
            if (vo.getIsClosed() != null) {
                vo.setIsClosed(EduDictUtils.getDictLabel("yes_or_no", vo.getIsClosed()));
            }
            
            // 获取教师姓名 - 使用teacherId作为teacherNo查询
            if (vo.getTeacherNo() != null) {
                EduTeacher teacher = eduTeacherService.selectEduTeacherByTeacherNo(vo.getTeacherNo().toString());
                if (teacher != null) {
                    vo.setTeacherName(teacher.getTeacherName());
                }
            }
            
            // 处理班级类别汉字映射
            if (vo.getClassCategory() != null) {
                String classCategoryName = EduDictUtils.getDictLabel("class_category", vo.getClassCategory());
                // 若没查询到相关的映射值，则classCategoryName=classCategory
                if (StringUtils.isEmpty(classCategoryName)) {
                    classCategoryName = vo.getClassCategory();
                }
                vo.setClassCategoryName(classCategoryName);
            }
            
            voList.add(vo);
        }
        tableDataInfo.setRows(voList);
        return tableDataInfo;
    }

    /**
     * 导出班级信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:class:export')")
    @Log(title = "班级信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduClass eduClass)
    {
        List<EduClass> list = eduClassService.selectEduClassList(eduClass);
        ExcelUtil<EduClass> util = new ExcelUtil<EduClass>(EduClass.class);
        util.exportExcel(response, list, "班级信息数据");
    }

    /**
     * 获取班级信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:class:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId)
    {
        return success(eduClassService.selectEduClassByClassId(classId));
    }

    /**
     * 新增班级信息
     */
    @PreAuthorize("@ss.hasPermi('manage:class:add')")
    @Log(title = "班级信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduClass eduClass)
    {
        return toAjax(eduClassService.insertEduClass(eduClass));
    }

    /**
     * 修改班级信息
     */
    @PreAuthorize("@ss.hasPermi('manage:class:edit')")
    @Log(title = "班级信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduClass eduClass)
    {
        return toAjax(eduClassService.updateEduClass(eduClass));
    }

    /**
     * 删除班级信息
     */
    @PreAuthorize("@ss.hasPermi('manage:class:remove')")
    @Log(title = "班级信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds)
    {
        return toAjax(eduClassService.deleteEduClassByClassIds(classIds));
    }
}
